﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Buccontrols;

namespace Buccaneer
{
    public partial class NavigateControl : PluginControl
    {
        BitmapButton bbn_back, bbn_up, bbn_forward, bbn_down;

        public NavigateControl()
        {
            InitializeComponent();

            SetStyle(ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);

            this.bbn_back = AddNavigateBack();
            this.bbn_back.Click += new EventHandler(bbn_back_Click);
            this.bbn_up = AddNavigateUp();
            this.bbn_up.Click += new EventHandler(bbn_up_Click);
            this.bbn_down = AddNavigateDown();
            this.bbn_down.Click += new EventHandler(bbn_down_Click);
            this.bbn_forward = AddNavigateForward();
            this.bbn_forward.Click += new EventHandler(bbn_forward_Click);
            this.Controls.Add(this.bbn_back);
            this.Controls.Add(this.bbn_up);
            this.Controls.Add(this.bbn_down);
            this.Controls.Add(this.bbn_forward);
        }

        public static void setFlatAppearance(ref BitmapButton bitmapButton)
        {
            bitmapButton.TabStop = true;
        }

        public static Image resizeImage(Image imgToResize, Size size)
        {
            return imgToResize.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero);
        }

        public static void loadImage(ref BitmapButton bitmapButton, string imageName)
        {
            try
            {
                Options.WriteComment(" -  loading image " + imageName + "...");
                bitmapButton.Image = Options.GetResource(imageName) as Image;
                Options.WriteComment("(i) Succeeded.");
            }
            catch (Exception ex)
            {
                Options.WriteComment("(X) An error occurred:" + ex.Message + "\nsource:" + ex.Source);
                MessageBox.Show("Error in loading the following image: " + imageName + "\n\nException info: " + ex.Message);
                bitmapButton.Image = null;
                return;
            }
            bitmapButton.LoadImages(imageName);
        }
        
        #region Navigation

        public static BitmapButton AddNavigateBack()
        {
            BitmapButton bitmapButton = new BitmapButton();
            bitmapButton.Location = new Point(0, 0);
            bitmapButton.TabIndex = 0;
            bitmapButton.Text = "";
            loadImage(ref bitmapButton, "navBack");
            bitmapButton.Size = bitmapButton.Image.Size;
            //bitmapButton.Disable();
            setFlatAppearance(ref bitmapButton);
            return bitmapButton;
        }

        public static BitmapButton AddNavigateUp()
        {
            BitmapButton bitmapButton = new BitmapButton();
            bitmapButton.Location = new Point(48, 0);
            bitmapButton.TabIndex = 0;
            bitmapButton.Text = "";
            loadImage(ref bitmapButton, "navUp");
            bitmapButton.Size = bitmapButton.Image.Size;
            //bitmapButton.Disable();
            setFlatAppearance(ref bitmapButton);
            return bitmapButton;
        }

        public static BitmapButton AddNavigateDown()
        {
            BitmapButton bitmapButton = new BitmapButton();
            bitmapButton.Location = new Point(48, 51);
            loadImage(ref bitmapButton, "navDown");
            bitmapButton.Size = bitmapButton.Image.Size;
            bitmapButton.TabIndex = 0;
            bitmapButton.Text = "";
            //bitmapButton.Disable();
            setFlatAppearance(ref bitmapButton);
            return bitmapButton;
        }

        public static BitmapButton AddNavigateForward()
        {
            BitmapButton bitmapButton = new BitmapButton();
            bitmapButton.Location = new Point(89, 0);
            bitmapButton.TabIndex = 0;
            bitmapButton.Text = "";
            loadImage(ref bitmapButton, "navForward");
            bitmapButton.Size = bitmapButton.Image.Size;
            //bitmapButton.Disable();
            setFlatAppearance(ref bitmapButton);
            return bitmapButton;
        }

        #endregion


        #region Eventhandlers

        void bbn_forward_Click(object sender, EventArgs e)
        {
            MainProgram.Plugins.Act("navigateforward", new string[] { "navigate.forward button clicked" });
        }

        void bbn_down_Click(object sender, EventArgs e)
        {
            MainProgram.Plugins.Act("navigatedown", new string[] { "navigate.down button clicked" });
        }

        void bbn_up_Click(object sender, EventArgs e)
        {
            MainProgram.Plugins.Act("navigateup", new string[] { "navigate.up button clicked" });
        }

        void bbn_back_Click(object sender, EventArgs e)
        {
            MainProgram.Plugins.Act("navigateback", new string[] { "navigate.back button clicked" });
        }

        #endregion
    }
}
